package com.example.easybuy.controller;

import com.example.easybuy.annotaion.CheckPermission;
import com.example.easybuy.entity.Product;
import com.example.easybuy.mappers.ProductMapper;
import com.example.easybuy.repository.ProductEsRepository;
import com.example.easybuy.service.ProductService;
import com.example.easybuy.util.AjaxResult;
import com.example.easybuy.util.SftpUtil;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.jcraft.jsch.SftpException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.util.Map;
import java.util.UUID;

@RestController
@RequestMapping("product")
public class ProductController {
    @Autowired
    private ProductService productService;

    @Autowired
    private ProductEsRepository repository;

    @Autowired
    ProductMapper productMapper;

    @RequestMapping("/byPage")
    public AjaxResult byPage(@RequestParam Map<String, Object> map) {
        return AjaxResult.success().add("pageInfo", productService.byPage(map));
    }

    @RequestMapping("/ProductELServiceImpl")
    public AjaxResult ProductELServiceImpl(@RequestParam Map<String, Object> map) {
        return AjaxResult.success().add("pageInfo", productService.elByPage(map));
    }

    @RequestMapping("/getPro")
    public AjaxResult getPro(@RequestParam Map<String, Object> map) {
        return AjaxResult.success().add("product", productService.getPro(map));
    }

    @PostMapping("/doAdd")
    @CheckPermission("1")
    public AjaxResult doAdd(@RequestParam Map<String, Object> map, MultipartFile fileItem) throws Exception {
        return productService.addPro(map, fileItem);
    }

    @RequestMapping("/doUpdate")
    @CheckPermission("1")
    public AjaxResult doUpdate(@RequestParam Map<String, Object> map, MultipartFile fileItem) throws Exception {
        return productService.updatePro(map, fileItem);
    }

    @RequestMapping("/doDel")
    @CheckPermission("1")
    public AjaxResult doDel(@RequestParam Map<String, Object> map) throws Exception {
        repository.deleteById(map.get("id").toString());
        return AjaxResult.success().add("status", productService.delPro(map));
    }
}
